<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>
<body>
<div th:text="${goods.goodsTitle}"></div>
<div th:text="'原价'+${goods.goodsPrice}" style="text-decoration-line: line-through"></div>
<div th:text="'秒杀价'+${goods.miaoshaPrice}"></div>
<div><img th:src="@{${goods.goodsImg}}" ></div>
<div id="btnDiv"><a id="doMiaosha" href="javascript:void(0)">秒杀</a></div>
<input type="hidden" id="last" th:value="${id}">
<script>
    var last = document.getElementById("last");
    var body = last.parentNode;
    var script = document.createElement("script");
    script.setAttribute("src", "/start.js?id="+last.value+"&t="+new Date().getTime());
    body.insertBefore(script, last)
</script>
<script type="text/javascript" src="/webjars/jquery/3.3.1/dist/jquery.min.js"></script>
<script>
    var t;
    setTimeout(function(){
        if(!started) {
            if(time>0) { // 没开始
                t = time;
                timer();
            } else {
                $("#btnDiv").html("秒杀已结束")
            }
        } else { // 开始了
            if(url) {
                $("#doMiaosha").one("click", function(){
                    $.post(url).done(function(json){
                        console.log(json);
                    }).fail(function(xhr){
                        console.log(JSON.parse(xhr.responseText));
                    });
                });
            }
        }
    },50);

    function timer() {
        t--;
        if(t==0){
            window.location.reload(); // 让页面刷新, 重新取js
        } else {
            $("#doMiaosha").text("剩余"+format(t));
            setTimeout(timer, 1000);
        }
    }

    function format(value) {
        var secondTime = parseInt(value);// 秒
        var minuteTime = 0;// 分
        var hourTime = 0;// 小时
        var dayTime =0; // 天
        if(secondTime > 60) {//如果秒数大于60，将秒数转换成整数
            //获取分钟，除以60取整数，得到整数分钟
            minuteTime = parseInt(secondTime / 60);
            //获取秒数，秒数取佘，得到整数秒数
            secondTime = parseInt(secondTime % 60);
            //如果分钟大于60，将分钟转换成小时
            if(minuteTime > 60) {
                //获取小时，获取分钟除以60，得到整数小时
                hourTime = parseInt(minuteTime / 60);
                //获取小时后取佘的分，获取分钟除以60取佘的分
                minuteTime = parseInt(minuteTime % 60);

                if(hourTime > 24) {
                    dayTime = parseInt(hourTime / 24);
                    hourTime = parseInt(hourTime % 24);
                }
            }
        }
        var result = "" + secondTime + "秒";

        if(minuteTime > 0) {
            result = "" + minuteTime + "分" + result;
        }
        if(hourTime > 0) {
            result = "" + hourTime + "小时" + result;
        }
        if(dayTime > 0) {
            result = "" + dayTime + "天" + result;
        }
        return result;
    }
</script>
</body>
</html>